package com.hmkj.web.controller.find;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.page.BasePageParams;
import com.hmkj.common.page.Pages;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.mapper.sysNotice.SystemNoticeMapper;
import com.hmkj.core.mapper.system.NoticeLogMapper;
import com.hmkj.core.model.fqa.FqaCategoryModel;
import com.hmkj.core.po.fqa.Fqa;
import com.hmkj.core.po.fqa.FqaCategory;
import com.hmkj.core.po.sysNotice.SystemNotice;
import com.hmkj.core.po.system.NoticeLog;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.fqa.FqaCategoryService;
import com.hmkj.core.service.fqa.FqaService;
import com.hmkj.core.service.sysNotice.SystemNoticeService;
import com.hmkj.core.service.system.NoticeLogService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "公告相关")
@Slf4j
@RestController
@RequestMapping(GenericController.BASE_API_URL + "/find")
public class AppDiscoverController extends GenericController<Integer, SystemNotice> {

    @Resource
    SystemNoticeService systemNoticeService;
    @Resource
    FqaCategoryService fqaCategoryService;
    @Resource
    FqaService fqaService;
    @Resource
    NoticeLogService noticeLogService;

    @ApiOperation(value = "获取公告列表", notes = "获取公告列表")
    @ResponseBody
    @RequestMapping(value = "/noticeList", method = RequestMethod.POST)
    public ResultEntity<Pages<SystemNotice>> getAnnounceList(@ModelAttribute BasePageParams basePageParams) {
        Map<String, Object> param = new HashMap<>();
        param.put("status", SystemNotice.STATUS.PUBLISHED.code);
        param.put("basePageParams", basePageParams);
        Pages<SystemNotice> pages = systemNoticeService.selectByPage(basePageParams,
                p -> ((SystemNoticeMapper) p).findCountForNoticeList(param),
                p -> ((SystemNoticeMapper) p).findListForNoticeList(param));
        return ok(pages);
    }

    @ApiOperation(value = "获取公告列详情", notes = "获取公告列详情")
    @ResponseBody
    @RequestMapping(value = "noticeDetail", method = RequestMethod.POST)
    public ResultEntity noticeDetail(@ApiParam("详情Id") @RequestParam(required = true) Integer id) {
       if(null == id){
           throw new BussinessException("参数有误");
       }
        return ok(systemNoticeService.selectByPrimaryKey(id));
    }

    @ApiOperation(value = "常见问题", notes = "常见问题")
    @ResponseBody
    @RequestMapping(value = "/helpCenter", method = RequestMethod.POST)
    public ResultEntity helpCenter() throws Exception {
        HashMap data =  new HashMap();
        String phone = redisService.get(ConfigNID.FOO_PHONE);
        data.put("phone", phone);
        //1.查询所有问题分类
        //2.查询各个分类下面的问题集合
        List<FqaCategory> categories = fqaCategoryService.selectAll();
        List<FqaCategoryModel> fqaData = new ArrayList<>();
        for (FqaCategory c : categories) {
            FqaCategoryModel model = new FqaCategoryModel();
            List<Fqa> byProperties = fqaService.select(new Fqa(t->{
                t.setCategoryId(c.getId());
            }));
            model.setName(c.getName());
            model.setRemark(c.getRemark());
            model.setFqaList(byProperties);
            fqaData.add(model);
        }
        data.put("categories", fqaData);
        return ok(data);
    }

    @ApiOperation(value = "关于我们", notes = "关于我们")
    @ResponseBody
    @RequestMapping(value = "/aboutUs", method = RequestMethod.POST)
    public ResultEntity aboutUs() throws Exception {
        String appName = redisService.get(ConfigNID.APP_NAME);
        String serviceTime = redisService.get(ConfigNID.SERVICE_TIME);
        String servicePhone = redisService.get(ConfigNID.SERVICE_PHONE);
        String qrName = redisService.get(ConfigNID.WECHAT_SERVICE);
        String version = redisService.get(ConfigNID.APP_VERSION);
        return ok(m->{
            m.put("appName",appName);
            m.put("serviceTime",serviceTime);
            m.put("servicePhone",servicePhone);
            m.put("qrName",qrName);
            m.put("version",version);
        });
    }

    @ApiOperation(value = "通知列表", notes = "通知列表")
    @ResponseBody
    @RequestMapping(value = "/messageList", method = RequestMethod.POST)
    public ResultEntity<Pages<NoticeLog>> messageList(@ModelAttribute BasePageParams basePageParams) {
        User user = getLoginUser();
        Map<String, Object> param = new HashMap<>();
        param.put("userId", user.getId());
        return ok(noticeLogService.selectByPage(basePageParams,
                q -> ((NoticeLogMapper) q).findCountForNoticeLogList(param)
                , q -> ((NoticeLogMapper) q).findListForNoticeLogList(param)));
    }

    @ApiOperation(value = "获取通知列表详情", notes = "获取通知列表详情")
    @ResponseBody
    @RequestMapping(value = "/messageDetail", method = RequestMethod.POST)
    public ResultEntity messageDetail(@ApiParam("详情Id") @RequestParam(required = true) Integer id) {
        if(null == id){
            throw new BussinessException("参数有误");
        }
        return ok(noticeLogService.selectByPrimaryKey(id));
    }

}
